package com.ruoyi.xmmb.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.xmmb.domain.Xmmb;
import com.ruoyi.xmmb.service.IXmmbService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目模板Controller
 *
 * @author huacai
 * @date 2024-08-12
 */
@RestController
@RequestMapping("/xmmb/xmmb")
public class XmmbController extends BaseController
{
    @Autowired
    private IXmmbService xmmbService;

    /**
     * 查询项目模板列表
     */
    @PreAuthorize("@ss.hasPermi('xmmb:xmmb:list')")
    @GetMapping("/list")
    public TableDataInfo list(Xmmb xmmb)
    {
        startPage();
        List<Xmmb> list = xmmbService.selectXmmbList(xmmb);
        return getDataTable(list);
    }

    /**
     * 导出项目模板列表
     */
    @PreAuthorize("@ss.hasPermi('xmmb:xmmb:export')")
    @Log(title = "项目模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Xmmb xmmb)
    {
        List<Xmmb> list = xmmbService.selectXmmbList(xmmb);
        ExcelUtil<Xmmb> util = new ExcelUtil<Xmmb>(Xmmb.class);
        util.exportExcel(response, list, "项目模板数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Xmmb> util = new ExcelUtil<Xmmb>(Xmmb.class);
        util.importTemplateExcel(response, "项目模板数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "项目模板", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('xmmb:xmmb:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Xmmb> util = new ExcelUtil<Xmmb>(Xmmb.class);
        InputStream inputStream = file.getInputStream();
        List<Xmmb> list = util.importExcel(inputStream );
        inputStream.close();
        int count = xmmbService.batchInsertXmmb(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取项目模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('xmmb:xmmb:query')")
    @GetMapping(value = "/{xmmbId}")
    public AjaxResult getInfo(@PathVariable("xmmbId") Integer xmmbId)
    {
        return success(xmmbService.selectXmmbByXmmbId(xmmbId));
    }

    /**
     * 新增项目模板
     */
    @PreAuthorize("@ss.hasPermi('xmmb:xmmb:add')")
    @Log(title = "项目模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Xmmb xmmb)
    {
        return toAjax(xmmbService.insertXmmb(xmmb));
    }

    /**
     * 修改项目模板
     */
    @PreAuthorize("@ss.hasPermi('xmmb:xmmb:edit')")
    @Log(title = "项目模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Xmmb xmmb)
    {
        return toAjax(xmmbService.updateXmmb(xmmb));
    }

    /**
     * 删除项目模板
     */
    @PreAuthorize("@ss.hasPermi('xmmb:xmmb:remove')")
    @Log(title = "项目模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{xmmbIds}")
    public AjaxResult remove(@PathVariable Integer[] xmmbIds)
    {
        return toAjax(xmmbService.deleteXmmbByXmmbIds(xmmbIds));
    }
}
